<?php
require_once ($INCLUDE_PATH . "./include/WebService2Db.php");
class ItemModel {
    public function saveItemInfo($s) {
        $s1 = 'id';
        $s2 = "'".$s['id']."'";
        foreach ($s as $key => $ss) {
            if($key != 'id') {
                $s1 .= ",".$key;
                $s2 .= ",'".$ss."'";
            }
        }
        //  $s1 .=",created_time";
        //  echo $date("Y-m-d H:i");
        //   echo $d;
        //  $s2 .=",'".$d."'";
        $DB=new DB_MYSQL;
        $sql="insert into `".PRE."_project`(".$s1.")" ."VALUES(".$s2.")";

        //  $sql="SELECT *  FROM `hh_user` WHERE `account` = '".$username."' AND `password` = '".$password."'";
        $query = $DB->insert($sql,DB);
        //  echo $DB->insert_id;
        return $DB->insert_id;
    }
    public function editItemInfo($s,$where="") {

        foreach ($s as $key => $ss) {

            if($key != "project_code") {

                $s1 .= ",".$key."='".$ss."'";

            }else {
                $s1 .= $key."='".$ss."'";
            }
        }
        $DB=new DB_MYSQL;
        $sql="update  `".PRE."_project` set ".$s1.$where;
        //  $sql="insert into `".PRE."_user`(".$s1.")" ."VALUES(".$s2.")";
        //  $sql="SELECT *  FROM `hh_user` WHERE `account` = '".$username."' AND `password` = '".$password."'";
        $query = $DB->update($sql,DB);
        //  echo $affected_rows;
        return $DB->affected_rows;
    }
    public function getListCount($where="") {
        $DB=new DB_MYSQL;
        $table=PRE."_project";
        $sum=$DB->count_records($table,"id",$where,DB);
        return $sum;

    // echo $sum;
    }
    public function getList($page,$liststart,$pageSize,$where="") {
        $DB=new DB_MYSQL;
        $sql="SELECT *  FROM `".PRE."_project` ".$where." LIMIT ".$liststart.",".$pageSize;
        $query = $DB->fetch_array($sql,DB);
        return $query;

    }
    public function delItem($id) {
        $DB=new DB_MYSQL;
        $sql = " delete from ".PRE."_project where id=".$id;
        $query = $DB->delete($sql, DB);
    }
    public  function getOneItem($id) {
        $DB=new DB_MYSQL;
        $DB->connect();
        $sql="SELECT *  FROM `".PRE."_project` WHERE `id` = '".$id."'";
        $query = $DB->query_first($sql);
        return $query;
    }
    public  function getCount($start_time,$end_time) {
        /*$DB=new DB_MYSQL;
        $where=" WHERE `send_time` >= UNIX_TIMESTAMP('".$start_time."') and 'send_time' <= UNIX_TIMESTAMP('".$end_time."')";

        $table =PRE."_send_email";
        $c[1] = $DB->count_records($table, "id", $where,DB);
        $where=" WHERE `open_ip` is not null and `send_time` >= UNIX_TIMESTAMP('".$start_time."') and 'send_time' <= UNIX_TIMESTAMP('".$end_time."')";
        $c[2] = $DB->count_records($table, "id", $where,DB);
        $where=" WHERE `click_ip` is not null and `send_time` >= UNIX_TIMESTAMP('".$start_time."') and 'send_time' <= UNIX_TIMESTAMP('".$end_time."')";
        $c[3] = $DB->count_records($table, "id", $where,DB);

        return $c;*/
		$table = PRE."_send_email";
		$s_time = strtotime($start_time);
		$e_time = strtotime($end_time);
        $DB=new DB_MYSQL;
		if($start_time==0 && $end_time==0){
			//$where = " where pid = '".$pid."' ";
			$c['all'] = $DB->count_records($table,"id",$where,DB);
			$where = " where state = 0";
			$c['nosend'] = $DB->count_records($table,"id",$where,DB);
			$where = " where state = 1";
			$c['send'] = $DB->count_records($table,"id",$where,DB);
		}
		else
		{
			//$one_day = 24*60*60;
			//$today = strtotime(date('Y-m-d'));
			//for($i=$days;$i>0;$i--)
			//{
				//$c[$i]['time'] = date('Y-m-d',$s_time);
				$d_where = " and send_time>{$s_time} and send_time<{$e_time}";
				$where = " where 1=1".$d_where;
				$c['all'] = $DB->count_records($table,"id",$where,DB);
				$where = " where state = 0".$d_where;
				$c['nosend'] = $DB->count_records($table,"id",$where,DB);
				$where = " where state = 1".$d_where;
				$c['send'] = $DB->count_records($table,"id",$where,DB);
			//}
		}
        return $c;
		
    }
    public function getUrlCount($start_time,$end_time,$url) {
        /*$DB=new DB_MYSQL;
        $table =PRE."_send_email";
        foreach ($url as $list) {

            $where=" WHERE email like '%".$list."' and `send_time` >= UNIX_TIMESTAMP('".$start_time."') and 'send_time' <= UNIX_TIMESTAMP('".$end_time."')";
            $c[$list][1] = $DB->count_records($table, "id", $where,DB);
            $where=" WHERE email like '%".$list."' and `open_ip` is not null and `send_time` >= UNIX_TIMESTAMP('".$start_time."') and 'send_time' <= UNIX_TIMESTAMP('".$end_time."')";
            $c[$list][2] = $DB->count_records($table, "id", $where,DB);
            $where=" WHERE email like '%".$list."' and `click_ip` is not null and `send_time` >= UNIX_TIMESTAMP('".$start_time."') and 'send_time' <= UNIX_TIMESTAMP('".$end_time."')";
            $c[$list][3] = $DB->count_records($table, "id", $where,DB);
        }
 
        return $c;
*/
		$table = PRE."_send_email";
		$s_time = strtotime($start_time);
		$e_time = strtotime($end_time);
        $DB=new DB_MYSQL;
		$domainlist = DomainModel::getList(0,0,10000);
		
		foreach ($domainlist as $key => $row) {
            $domain = $row['url'];
        	$c[$i]['domain']=$domain;
			//SELECT * FROM `hh_send_email` WHERE instr(email,'@163.com')
			$domain_where = " and instr(email,'@{$domain}')";
			if($start_time==0 && $end_time==0){
				$where = " where 1=1 ".$domain_where;
				$c[$i]['all'] = $DB->count_records($table,"id",$where,DB);
				$where = " where state = 0".$domain_where;
				$c[$i]['nosend'] = $DB->count_records($table,"id",$where,DB);
				$where = " where state = 1".$domain_where;
				$c[$i]['send'] = $DB->count_records($table,"id",$where,DB);
			}
			else
			{
				//$one_day = 24*60*60;
				//$today = strtotime(date('Y-m-d'));
				//for($i=$days;$i>0;$i--)
				//{
					//$c[$i]['time'] = date('Y-m-d',$s_time);
					$d_where = " and send_time>{$s_time} and send_time<{$e_time}";
					$where = " where 1=1".$d_where.$domain_where;
					$c[$i]['all'] = $DB->count_records($table,"id",$where,DB);
					$where = " where state = 0".$d_where.$domain_where;
					$c[$i]['nosend'] = $DB->count_records($table,"id",$where,DB);
					$where = " where state = 1".$d_where.$domain_where;
					$c[$i]['send'] = $DB->count_records($table,"id",$where,DB);
				//}
			}
			$i++;
		}
        return $c;
		
    }
	
	public function urlCount($pid,$days=0) {
        $table = PRE."_send_email";
        $DB=new DB_MYSQL;
		if($days==0){
			$where = " where pid = '".$pid."' ";
			$c['all'] = $DB->count_records($table,"id",$where,DB);
			$where = " where pid = ".$pid." and state = 0";
			$c['nosend'] = $DB->count_records($table,"id",$where,DB);
			$where = " where pid = ".$pid." and state = 1";
			$c['send'] = $DB->count_records($table,"id",$where,DB);
		}
		else
		{
			$one_day = 24*60*60;
			$today = strtotime(date('Y-m-d'));
			for($i=$days;$i>0;$i--)
			{
				$s_time = $today - $one_day*$i;
				$e_time = $today - $one_day*($i-1);
				$c[$i]['time'] = date('Y-m-d',$s_time);
				$d_where = " and send_time>{$s_time} and send_time<{$e_time}";
				$where = " where pid = '".$pid."'".$d_where;
				$c[$i]['all'] = $DB->count_records($table,"id",$where,DB);
				$where = " where pid = ".$pid." and state = 0".$d_where;
				$c[$i]['nosend'] = $DB->count_records($table,"id",$where,DB);
				$where = " where pid = ".$pid." and state = 1".$d_where;
				$c[$i]['send'] = $DB->count_records($table,"id",$where,DB);
			}
		}
        return $c;
    }
}
?>
